//
//  CustomButtonItem.m
//  CloudsOutpatient
//
//  Created by zouzhisheng on 16/1/30.
//  Copyright © 2016年 HC. All rights reserved.
//

#import "CustomButtonItem.h"
#import "QLKStretchableImageCache.h"
#define KSImageScale 0.5
@implementation CustomButtonItem
- (id)initWithFrame:(CGRect)frame norImage:(NSString*)norImage selectedImage:(NSString*)selectedImage titile:(NSString*)title;
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // 1、顶部图片
        self.imageView.contentMode = UIViewContentModeCenter;
        [self setImage:[UIImage imageNamed:norImage] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
        
        // 2、设置文字
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:itemTitleColor forState:UIControlStateNormal];
        [self setTitleColor:selectedItemTitleColor forState:UIControlStateSelected];
        self.titleLabel.font=itemTitleFont;
        [self addSubview:self.badgeValueView];

    }
    return self;
}
#pragma mark -设置小红圈里的数字
- (void)setItemBadgeNumber:(NSInteger)number
{
    if (number != 0) {
        
        if (self.badgeValueView.hidden) {
            
            self.badgeValueView.hidden = NO;
        }
        [self.badgeValueView setTitle:[NSString stringWithFormat:@"%ld",(long)number] forState:(UIControlStateNormal)];
        NSString *badgeStr=[NSString stringWithFormat:@"%ld",(long)number];
        if (badgeStr.length>1) {
            [_badgeValueView setBackgroundImage:[[QLKStretchableImageCache sharedInstance]imageNamed:badgeValueViewImageName leftCapwidth:7.5 topCapHeight:7.5] forState:(UIControlStateNormal)];
            _badgeValueView.width=badgeValueViewWH+4;
        }else{
            [_badgeValueView setBackgroundImage:[UIImage imageNamed:badgeValueViewImageName] forState:(UIControlStateNormal)];
            _badgeValueView.width=badgeValueViewWH;
        }
    }
    else{
        
        self.badgeValueView.hidden = YES;
    }
}

#pragma mark - 设置按钮内部图片和文字的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat W = contentRect.size.width;
    CGFloat H = contentRect.size.height *KSImageScale;
    if (self.isNormal) {
        return CGRectMake(0, 0, contentRect.size.width, contentRect.size.height);
    }else if (self.forHome){
       return CGRectMake(0, -10, contentRect.size.width, contentRect.size.height);
    }else{
         return CGRectMake(0, 5, W, H);
    }
   
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat W = contentRect.size.width;
    CGFloat H = contentRect.size.height *KSImageScale;
    CGFloat y = contentRect.size.height - H;
    if (self.forHome) {
        return   CGRectMake(0, y+10, W, H);
    }else{
        return CGRectMake(0, y+2, W, H);
    }
   
}
- (UIButton *)badgeValueView
{
    if (_badgeValueView == nil) {
        
        CGFloat x = self.width/2+5;
        CGFloat y = 0;
        _badgeValueView = [[UIButton alloc] initWithFrame:CGRectMake(x, y,badgeValueViewWH, badgeValueViewWH)];
        [_badgeValueView setBackgroundImage:[UIImage imageNamed:badgeValueViewImageName] forState:(UIControlStateNormal)];
        _badgeValueView.titleLabel.font = badgeValueFont;
        [_badgeValueView setTitleColor:badgeValueColor forState:(UIControlStateNormal)];
        //_badgeValueView.hidden = YES;
        _badgeValueView.adjustsImageWhenHighlighted = NO;
    }
    return  _badgeValueView;
}
@end
